package com.mibotiquin.menu;

import java.util.ArrayList;
import java.util.Iterator;

import android.os.Bundle;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.mibotiquin.R;
import com.mibotiquin.DbHelper.*;
import com.mibotiquin.alarmas.AlarmContainer;
import com.mibotiquin.alarmas.CadNot;
import com.mibotiquin.alarmas.CalendarView;
import com.mibotiquin.alarmas.ItemCad;
import com.mibotiquin.config.Configuracion;
import com.mibotiquin.map.MiMapaActivity;

import com.mibotiquin.misfarmacos.FragmentTabs;
import com.mibotiquin.scaner.scaner;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class Principal extends SherlockActivity {
	
	private boolean soltado=false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_principal);
		
		//Esto es una prueba. Clase BaseUtil modificada.
		final BaseUtil bd = BaseUtil.getInstance(this);
		loadNotifiactions();
		AlarmContainer alCon= AlarmContainer.getInstance();
		alCon.init(this);
		
		final ImageView btnFarmacos = (ImageView)findViewById(R.id.ivFarmacos); 
		
		btnFarmacos.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				
				bd.initListFarmacos(v);
				
				Intent intent = new Intent(Principal.this, FragmentTabs.class);
				Bundle bundle = new Bundle();
				intent.putExtras(bundle);
				startActivity(intent);
			}
        	
        	
        });

		final ImageView btnAlarmas =(ImageView)findViewById(R.id.imageView1);
		btnAlarmas.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Principal.this, CalendarView.class);
				Bundle bundle = new Bundle();
				intent.putExtras(bundle);
				soltado=false;								
				startActivity(intent);
				
			}
		});
		
		

	
		final ImageView botonbuscar = (ImageView)findViewById(R.id.imageView2);
		
		botonbuscar.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				
				Intent intent = new Intent(Principal.this,scaner.class);
				Bundle bundle = new Bundle();
				intent.putExtras(bundle);
				
				startActivity(intent);
			}
		});
    
		
	
		final ImageView btnMapa = (ImageView)findViewById(R.id.ivFarmacias);
		
		btnMapa.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				
				Intent intent = new Intent(Principal.this, MiMapaActivity.class);
				Bundle bundle = new Bundle();
				intent.putExtras(bundle);				
				
				startActivity(intent);
				
				
			}
        	
        	
        });
		
		final ImageView btnConfig = (ImageView)findViewById(R.id.imageView4);
		
		btnConfig.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				
				Intent intent = new Intent(Principal.this, Configuracion.class);
				Bundle bundle = new Bundle();
				intent.putExtras(bundle);
				
				startActivity(intent);
			}
        	
        	
        });
		
	
	
	
		
		
	}
	private void loadNotifiactions() {

		//Read the notifications from the database
			String query = "SELECT * FROM cad";
			ItemCad item = new ItemCad();
			BaseUtil bd = BaseUtil.getInstance(this);
			ArrayList<DatabaseItem> ar = bd.consulta(query, item);
			
			Iterator<DatabaseItem> it = ar.iterator();
			
			while (it.hasNext()) {
				
				item = (ItemCad)it.next();
				String[] data = item.getDatosArray();
				
				CadNot cadNot = new CadNot(data[1], data[2], Integer.parseInt(data[3].split("/")[0]), Integer.parseInt(data[3].split("/")[1]),
						this);
				cadNot.init();
			}
			
	}


	
	

}